//
// xmlapp.h
// xmlint
// An XML-based interface for the Protea Project / xmlapp.h implements the AppInterface object
// Copyright (C) 2003, 2004, 2005 eXegeSys, Inc.
// Copyright (C) 2008 Bruce A. James
//
// This program is part of the Protea Project.
//
// The Protea Project is free software; you can redistribute it and/or modify it 
// under the terms of the GNU General Public License as published by the 
// Free Software Foundation, either version 3 of the License, or (at your 
// option) any later version.
//
// The Protea Project is distributed in the hope that it will be useful, but 
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
// for more details.
// 
// You should have received a copy of the GNU General Public License along 
// with this program.  If not, see <http://www.gnu.org/licenses/>.
// 
// Please send correspondence to:
// 
// theproteaproject@gmail.com
//

#ifndef __PROTEA_XMLAPP_H__
#define __PROTEA_XMLAPP_H__

#include "../AppHandler/parms_xml.h"
#include "../OSDep/service.h"

class XCXMLApplicationList {
public:
	XCAppInterface* app;
	XCXMLApplicationList* next;
};



struct socket_information {
	XCSocketTransport* transport;
	int counter;
	THREAD_HANDLE thread;

	// Double-linked list
	socket_information* next;
	socket_information* prev;
};


struct trans_socket_type {
	socket_information* socket;
	int trans_id;
	int app_nbr;
	int session;

	// Double-linked list
	trans_socket_type* next;
	trans_socket_type* prev;
};



// XCXMLAppInterface class defnitions
class XCXMLAppInterface : public XCAppInterface, public XCService, public parms_reader {
public:
	XCXMLAppInterface( char* name, char* dispname, char* desc, void* func, char* deps );
	~XCXMLAppInterface();

	void Startup();
	void SendTransactionXML( socket_information *socket, XCTransactionInfo* txn );

	XCCriticalSection crit;
	XCCriticalSection app_list_crit;
	XCCriticalSection socket_info;

	// Protea events
	void OnDroppedConnection( XCMsgPortPtr port );
	void OnUserMessage( char* message );
	XCTransactionInfo* OnTransaction( XCTransactionInfo* transaction );
	XCTransactionInfo* OnQuery( XCTransactionInfo* transaction );
	void HandleSecurityChange( XCToolsMessage* msg );

	// Daemon functions
	void OnInit();
	void OnRun();
	void OnSignalStop();
	void OnCleanup();

	long tracing_mode;

	void GetApplicationTransaction( XCAppInterface* &myapp, int app_number, XCTransactionInfo* &info );
	XCAppInterface* GetApplication( int app_number );

	trans_socket_type* FindSocketTrans( int trans_id, int& app_nbr );
	void AddSocketTrans( socket_information* info, int trans_id, int app_nbr, int session );
	void RemoveSocketTrans( trans_socket_type* temp );
	void AbortSocketTrans( socket_information* info );

	void AddSocketInfo( socket_information* info );
	void RemoveSocketInfo( socket_information* info );

	XCSocketTransport *listen_socket;
	int listen_port;

	char* hostname;
	long gcs_port;

	THREAD_HANDLE listen_thread;

private:
	XCSocketTransport *newsocket;
	XCXMLApplicationList *app_list;
	socket_information* socket_thread_list;
	trans_socket_type* trans_socket_list;
};


extern XCXMLAppInterface xmlint;

#endif
